﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

namespace DocFactoryWebLibrary
{
    public class XMLParser
    {
        static Dictionary<string, string> Values = new Dictionary<string, string>();

        public static string ReadXmlDocument(string TemplateFilePath, string TempPath)
        {
            // Create a new XmlDocument   
            XmlDocument doc = new XmlDocument();

            // Load data   
            doc.Load(TemplateFilePath);

            ////// Set up namespace manager for XPath   
            XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
            ns.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/wordml");

            // Get Literal Text with XPath   
            XmlNodeList nodes = doc.SelectNodes("//w:t", ns);

            // An Opennig Tag Was Found
            bool StartTag = false;

            foreach (XmlNode node in nodes)
            {
                node.InnerText = InnerMatches(node.InnerText);
                int EndTagPos = node.InnerText.IndexOf(">>");
                if (EndTagPos != -1)
                {
                    StartTag = false;
                    node.InnerText = node.InnerText.Remove(EndTagPos, 2);
                }

                if (StartTag)
                {
                    node.InnerText = InnerMatches("<<" + node.InnerText + ">>");
                }

                int StartTagPos = node.InnerText.IndexOf("<<");
                if (StartTagPos != -1)
                {
                    node.InnerText = node.InnerText.Remove(StartTagPos, 2);
                    StartTag = true;
                }

                if (node.InnerText.Length >= StartTagPos + 2)
                {
                    if (node.InnerText.IndexOf(">>", StartTagPos + 2) != -1)
                        StartTag = false;
                }

            }
            
            string strFileName = Path.GetRandomFileName();
            XmlTextWriter Bob = new XmlTextWriter(TempPath +  strFileName + ".xml ", null);

            doc.WriteTo(Bob);
            Bob.Flush();
            Bob.Close();

            return  strFileName + ".xml";

        }

        static string InnerMatches(string Input)
        {
            MatchEvaluator MatchEval = new MatchEvaluator(HashTableReplace);
            Regex objNotNaturalPattern = new Regex("<<(.*?)>>");

            Input = objNotNaturalPattern.Replace(Input, MatchEval);

            return Input;
        }

        static string HashTableReplace(Match m)
        {
            string ReturnString = "TagButNoMatch";

            if (Values.ContainsKey(m.Value.ToUpper()))
                ReturnString = (string)Values[m.Value.ToUpper()];

            return ReturnString;
        }

        public static void MergeData(Dictionary<string, string> objInput)
        {
            Values = objInput;
        }

        public static bool PopulateData()
        {
            bool Sucess = false;
            Values.Add("<<ANDY>>", "GIMPY gimp gimp boy");
            Values.Add("<<TITLE>>", "TITLE_TAG");
            Values.Add("<<REF>>", "TITLE_TAG_GIMP");
            Sucess = true;

            return Sucess;
        }
    }
}
